package com.ruike.alisurface.ui;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.hjq.xtoast.XToast;
import com.ruike.alisurface.R;
import com.ruike.alisurface.base.BaseAty;
import com.ruike.alisurface.bean.ProductDetailsBean;
import com.ruike.alisurface.https.ShopApiHttp;
import com.ruike.alisurface.utils.CountTimeUtils;
import com.ruike.alisurface.utils.GsonUtils;
import com.ruike.alisurface.utils.PxUtils;
import com.voodoo.lib_http.ApiListener;
import com.voodoo.lib_logcat.L;
import com.voodoo.lib_utils.imageLoader.GlideImageLoader;
import com.youth.banner.Banner;
import com.youth.banner.BannerConfig;
import com.youth.banner.Transformer;
import com.youth.banner.listener.OnBannerListener;
import com.youth.banner.loader.ImageLoader;

import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

public class ShopDetails_Aty extends BaseAty {
    @BindView(R.id.to_back)
    ImageView toBack;
    @BindView(R.id.to_times)
    TextView toTimes;
    @BindView(R.id.details_name)
    TextView detailsName;
    @BindView(R.id.details_price)
    TextView detailsPrice;
    @BindView(R.id.details_is_true_name_tv)
    TextView detailsIsTrueNameTv;
    @BindView(R.id.details_is_remote_tv)
    TextView detailsIsRemoteTv;
    @BindView(R.id.details_is_insurance_tv)
    TextView detailsIsInsuranceTv;
    @BindView(R.id.details_is_true_prescription_tv)
    TextView detailsIsTruePrescriptionTv;
    @BindView(R.id.details_alias)
    TextView detailsAlias;
    @BindView(R.id.details_spec)
    TextView detailsSpec;
    @BindView(R.id.details_manufacturer)
    TextView detailsManufacturer;
    @BindView(R.id.details_product_type)
    TextView detailsProductType;
    @BindView(R.id.details_approval_no)
    TextView detailsapprovalno;
    @BindView(R.id.details_codeBar)
    TextView detailsCodeBar;
    @BindView(R.id.details_main_components)
    TextView detailsMainComponents;
    @BindView(R.id.details_functional)
    TextView detailsFunctional;
    @BindView(R.id.details_usage)
    TextView detailsUsage;
    @BindView(R.id.details_indication)
    TextView detailsIndication;

    @BindView(R.id.adv_image_banner)
    Banner banner;

    @BindView(R.id.details_addcar)
    TextView details_addcar;
    List<String> aux_photoList = new ArrayList<>();
    List<ProductDetailsBean.DataBean> dataBeanList = new ArrayList<>();

    @Override
    protected int getLayoutResId() {
        return R.layout.app_prouct_details;
    }

    @Override
    protected void initViews() {
        initTitleBar(false);
        startTime(120);
        banner.setOnBannerListener(new OnBannerListener() {
            @Override
            public void OnBannerClick(int position) {
                showScalceImage(aux_photoList.get(position));
            }
        });
        toBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                setResult(Activity.RESULT_OK);
                finish();
            }
        });
        details_addcar.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                setResult();
            }
        });

    }

    CountTimeUtils countTimeUtils;

    public void startTime(long mStartTime) {
        mCancelTime();
        countTimeUtils = new CountTimeUtils(mStartTime) {
            @Override
            public void onTick(long mMillisUntilFinished) {
                toTimes.setText(String.format("%d", mMillisUntilFinished)+"s");
            }

            @Override
            public void onFinish() {
                L.i("onFinish=", "onFinish    ");
                setResult(Activity.RESULT_OK);
                finish();
            }
        };
        countTimeUtils.start();
    }


    public void mCancelTime() {
        if (countTimeUtils != null) {
            countTimeUtils.cancel();
            countTimeUtils = null;
        }
    }

    String product_id = "";
    Double price;

    public void setResult() {
        Intent intent = new Intent();
        intent.putExtra("product_id", product_id);
        intent.putExtra("price", price);
        setResult(Activity.RESULT_OK, intent);
        finish();
    }

    @Override
    protected void initData() {
        Bundle bundle = getIntent().getExtras();
        if (bundle != null) {
            product_id = bundle.getString("product_id");
            price = bundle.getDouble("price");
            showProgressDialog("加载数据中，请等待...");
            ShopApiHttp.get_medicine_details(product_id, price + "", new ApiListener() {
                @Override
                public void onRequestStart(String requestUrl, int requestId) {

                }

                @Override
                public void onRequestComplete(String requestUrl, int requestId) {
                    removeProgressDialog();
                }

                @Override
                public void onSuccess(String requestUrl, JSONObject requestJson, int requestId) {
                    L.i("详情----" + requestJson.toString());
                    ProductDetailsBean detailsBean = GsonUtils.fromJson(requestJson.toString(), ProductDetailsBean.class);
                    dataBeanList.add (detailsBean.getData() );
                    setDetails();
                }

                @Override
                public void onError(String requestUrl, String errorMsg, String resultData, int requestId) {

                }
            });
        }

    }

    public class MyImageLoader extends ImageLoader {
        @Override
        public void displayImage(Context context, Object path, ImageView imageView) {
            imageView.setScaleType(ImageView.ScaleType.FIT_CENTER); // XY轴拉伸铺满
            // imageView.setScaleType(ImageView.ScaleType.FIT_CENTER); // 中心缩放等比例加载
            GlideImageLoader.getInstance().loadImage(context, ((String) path), imageView);
        }
    }


    public void setDetails() {
        if (dataBeanList.size() > 0) {
            ProductDetailsBean.DataBean dataBean = dataBeanList.get(0);

            aux_photoList.addAll(dataBean.getAux_photo_ls());
            // 设置banner样式 不显示指示器和标题
            banner.setBannerStyle(BannerConfig.CIRCLE_INDICATOR);
            // 设置图片加载器
            banner.setImageLoader(new MyImageLoader());
            // 设置图片集合
            banner.setImages(aux_photoList);
            // 设置banner动画效果
            banner.setBannerAnimation(Transformer.Default);
            // 设置自动轮播，默认为true
            banner.isAutoPlay(true);
            // 设置轮播时间
            banner.setDelayTime(15000);
            // banner设置方法全部调用完毕时最后调用
            banner.start();

            detailsName.setText(dataBean.getName());
            detailsPrice.setText(String.format("￥ %s", dataBean.getPrice()));
            detailsIsTrueNameTv.setVisibility(dataBean.isIs_true_name() ? View.VISIBLE : View.GONE);
            detailsIsRemoteTv.setVisibility(dataBean.isIs_remote() ? View.VISIBLE : View.GONE);
            detailsIsInsuranceTv.setVisibility(dataBean.isIs_insurance() ? View.VISIBLE : View.GONE);
            detailsIsTruePrescriptionTv.setVisibility(dataBean.isIs_prescription() ? View.VISIBLE : View.GONE);


            detailsAlias.setText(!TextUtils.isEmpty(dataBean.getAlias()) ? dataBean.getAlias() : "");

            detailsSpec.setText(dataBean.getSpec());
            detailsManufacturer.setText(dataBean.getName());
            detailsProductType.setText(new StringBuilder().append(dataBean.getLv1_name()).append("/").
                    append(dataBean.getLv2_name()).append("/").append(dataBean.getLv3_name()).toString());
            detailsapprovalno.setText(dataBean.getApproval_no());
            detailsCodeBar.setText(dataBean.getCodeBar());


            detailsMainComponents.setText(dataBean.getMain_components());
            detailsFunctional.setText(dataBean.getFunctional());
            detailsUsage.setText(dataBean.getUsage());
            detailsIndication.setText(dataBean.getIndication());

        }
    }


    /**
     * 图片放大
     */
    public void showScalceImage(String urlpath) {
        View toastView = LayoutInflater.from(this).inflate(R.layout.dialog_images, null);
        XToast xToast = new XToast(this).setView(toastView).setAnimStyle(android.R.style.Animation_Toast);
        xToast.setWidth(PxUtils.dp2px(this, 960));
        xToast.setHeight(PxUtils.dp2px(this, 1160));
        xToast.setOutsideTouchable(false);
        xToast.setGravity(Gravity.CENTER_VERTICAL);
        xToast.show();
        ImageView imgae_delimg = toastView.findViewById(R.id.imgae_del_img);
        ImageView imgae_scaleimg = toastView.findViewById(R.id.imgae_scaleimg);
        GlideImageLoader.getInstance().loadImage(this, urlpath, imgae_scaleimg);
        imgae_delimg.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                xToast.cancel();
            }
        });
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        mCancelTime();
        dataBeanList.clear();
        dataBeanList = null;
        if (banner != null) {
            banner.releaseBanner();
            banner = null;
        }
    }
}
